package br.com.bancodecurriculos.pessoa.comum.dao.impl;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.com.bancodecurriculos.pessoa.comum.dao.IEmailDAOHelper;
import br.com.bancodecurriculos.pessoa.comum.dominio.Email;
import br.com.bancodecurriculos.pessoa.comum.dominio.Email.TipoEmail;
import br.com.bancodecurriculos.util.dao.JDBCConnectionFactory;
import br.com.bancodecurriculos.util.exception.RegistroExistenteException;
import br.com.bancodecurriculos.util.exception.RegistroInexistenteException;

public class EmailDAOHelper implements IEmailDAOHelper {

	private static final String SQL_TIPOEMAIL_GET_BY_NAME = "SELECT * FROM tipoemail WHERE nome = ?";
	private static final String SQL_TIPOEMAIL_GET_ALL = "SELECT * FROM tipoemail";

	private static final String SQL_EMAIL_INSERT = "INSERT INTO (valor, id_tipoendereco, id_pessoa) VALUES (?, ?, ?)";
	private static final String SQL_EMAIL_UPDATE = "UPDATE email SET valor = ?, id_tipoendereco = ? WHERE id = ?";
	private static final String SQL_EMAIL_GET_BY_ID = "SELECT * FROM email WHERE id = ?";
	private static final String SQL_EMAIL_GET_BY_VALOR = "SELECT id FROM email WHERE valor = ?";
	private static final String SQL_EMAIL_GET_ID_PESSOA = "SELECT email.id AS id, email.valor AS valor, email.id_tipoemail AS id_tipoemail,"
			+ " tipoemail.nome AS nome_tipoemail FROM email INNER JOIN tipoemail ON email.id_tipoemail = tipoemail.id WHERE email.id_pessoa = ?";
	private static final String SQL_EMAIL_DELETE = "DELETE FROM email WHERE id = ?";

	private static final String EMAIL_EXISTENTE_EXCEPTION = "Email j� existe!";
	private static final String EMAIL_INEXISTENTE_EXCEPTION = "Email inexistente!";

	@Deprecated
	@Override
	public void save(Email t) throws ClassNotFoundException, SQLException,
			RegistroExistenteException, IOException {
		// TODO Auto-generated method stub

	}

	@Override
	public void save(Email email, long idPessoa) throws ClassNotFoundException,
			SQLException, IOException, RegistroExistenteException {
		if (this.getByValor(email.getValor()) == null) {
			TipoEmail tipoEmail = this.getTipoEmailByNome(email.getTipoEmail()
					.getNome());
			PreparedStatement preparedStatement = JDBCConnectionFactory
					.getInstance().getConnection()
					.prepareStatement(SQL_EMAIL_INSERT);
			preparedStatement.setString(1, email.getValor());
			preparedStatement.setLong(2, tipoEmail.getId());
			preparedStatement.setLong(3, idPessoa);
			preparedStatement.executeUpdate();
			preparedStatement.close();
		} else {
			throw new RegistroExistenteException(EMAIL_EXISTENTE_EXCEPTION);
		}
	}

	@Override
	public void update(Email t) throws ClassNotFoundException, SQLException,
			RegistroInexistenteException, IOException {
		if (this.getById(t.getId()) != null) {
			TipoEmail tipoEmail = this.getTipoEmailByNome(t.getTipoEmail()
					.getNome());
			PreparedStatement preparedStatement = JDBCConnectionFactory
					.getInstance().getConnection()
					.prepareStatement(SQL_EMAIL_UPDATE);
			preparedStatement.setString(1, t.getValor());
			preparedStatement.setLong(2, tipoEmail.getId());
			preparedStatement.executeUpdate();
			preparedStatement.close();
		} else {
			throw new RegistroInexistenteException(EMAIL_INEXISTENTE_EXCEPTION);
		}
	}

	@Override
	public Email getById(Long id) throws ClassNotFoundException, SQLException,
			IOException {
		Email email = null;
		PreparedStatement preparedStatement = JDBCConnectionFactory
				.getInstance().getConnection()
				.prepareStatement(SQL_EMAIL_GET_BY_ID);
		preparedStatement.setLong(1, id);
		ResultSet resultSet = preparedStatement.executeQuery();
		if (resultSet.next()) {
			email = new Email();
			email.setId(resultSet.getLong("id"));
			email.setValor(resultSet.getString("valor"));
		}
		return email;
	}

	@Override
	public void delete(Long id) throws ClassNotFoundException, SQLException,
			RegistroInexistenteException, IOException {
		if (this.getById(id) != null) {
			PreparedStatement preparedStatement = JDBCConnectionFactory
					.getInstance().getConnection()
					.prepareStatement(SQL_EMAIL_DELETE);
			preparedStatement.setLong(1, id);
			preparedStatement.executeUpdate();
			preparedStatement.close();
		} else {
			throw new RegistroInexistenteException(EMAIL_INEXISTENTE_EXCEPTION);
		}
	}

	@Override
	public List<Email> getByIdPessoa(long id) throws ClassNotFoundException,
			SQLException, IOException {
		List<Email> emails = new ArrayList<Email>();
		PreparedStatement preparedStatement = JDBCConnectionFactory
				.getInstance().getConnection()
				.prepareStatement(SQL_EMAIL_GET_ID_PESSOA);
		preparedStatement.setLong(1, id);
		ResultSet resultSet = preparedStatement.executeQuery();
		while (resultSet.next()) {
			Email email = new Email();
			email.setId(resultSet.getLong("id"));
			email.setValor(resultSet.getString("valor"));
			TipoEmail tipoEmail = email.new TipoEmail(
					resultSet.getString("nome_tipoemail"));
			tipoEmail.setId(resultSet.getLong("id_tipoemail"));
			emails.add(email);
		}
		return null;
	}

	private Email getByValor(String valor) throws ClassNotFoundException,
			SQLException, IOException {
		Email email = null;
		PreparedStatement preparedStatement = JDBCConnectionFactory
				.getInstance().getConnection()
				.prepareStatement(SQL_EMAIL_GET_BY_VALOR);
		preparedStatement.setString(1, valor);
		ResultSet resultSet = preparedStatement.executeQuery();
		if (resultSet.next()) {
			email = new Email();
			email.setId(resultSet.getLong("id"));
		}
		preparedStatement.close();
		resultSet.close();
		return email;
	}

	@Override
	public TipoEmail getTipoEmailByNome(String nome)
			throws ClassNotFoundException, SQLException, IOException {
		TipoEmail tipoEmail = null;
		PreparedStatement preparedStatement = JDBCConnectionFactory
				.getInstance().getConnection()
				.prepareStatement(SQL_TIPOEMAIL_GET_BY_NAME);
		preparedStatement.setString(1, nome);
		ResultSet resultSet = preparedStatement.executeQuery();
		if (resultSet.next()) {
			Email email = new Email();
			tipoEmail = email.new TipoEmail(resultSet.getString("nome"));
			tipoEmail.setId(resultSet.getLong("id"));
		}
		preparedStatement.close();
		resultSet.close();
		return tipoEmail;
	}

	@Override
	public List<TipoEmail> getAllTipoEmail() throws ClassNotFoundException,
			SQLException, IOException {
		List<TipoEmail> tipoEmails = new ArrayList<TipoEmail>();
		PreparedStatement preparedStatement = JDBCConnectionFactory
				.getInstance().getConnection()
				.prepareStatement(SQL_TIPOEMAIL_GET_ALL);
		ResultSet resultSet = preparedStatement.executeQuery();
		Email email = new Email();
		while (resultSet.next()) {
			TipoEmail tipoEmail = email.new TipoEmail();
			tipoEmail = email.new TipoEmail(resultSet.getString("nome"));
			tipoEmail.setId(resultSet.getLong("id"));
			tipoEmails.add(tipoEmail);
		}
		preparedStatement.close();
		resultSet.close();
		return tipoEmails;
	}
}
